# -*- coding: utf-8-*-
import xlwt
import sys,os
import projectUtils
import matplotlib.pyplot as plt
import DrawPic

reload(sys)
sys.setdefaultencoding("utf-8")
def initBook():
    return xlwt.Workbook(encoding="utf-8", style_compression=0)
def createSheet():
    return book.add_sheet(u"测试报告", cell_overwrite_ok=True)
def write(row,colum,value,style=None):
    if style is None:
        sheet.write(row, colum, value)
    else:
        sheet.write(row, colum, value,style)

def writeRow(row,columStart,valueList,style):
    for value in valueList:
        write(row,columStart,value,style)
        columStart += 1


def write_merge(row,rlen,colum,clen,value,style):
    sheet.write_merge(row,rlen,colum,clen,value,style)

def saveBook(filename):
    book.save(filename + ".xls")

def insertPic(picName,row,colum):
    sheet.insert_bitmap(picName, row, colum)

def generateReport(productName,version,testscope,issueSummary,bugDetail,openBug,allBug,testenv):
    style = StyleUtils()
    reportTitleStyle = style.reportTitleStyle
    testscopeStyle = style.testscopeStyle
    scopecontentStyle = style.scopecontentStyle
    cycleStyle = style.cycleStyle
    issueSummaryStyle=style.issueSummaryStyle
    tableStyle = style.tableStyle
    passStyle = style.passStyle
    bugStyle = style.bugStyle
    hyperlinkStyle = style.hyperlinkStyle
    testResultStyle = style.testResultStyle
    testTitle = style.testTitle

    titleStartRow = 2
    startColum = 1
    title =	productName+version+u"测试报告"
    write_merge(titleStartRow,2,startColum,10,title,reportTitleStyle)

    summaryStartRow = titleStartRow + 3
    writeRow(summaryStartRow,startColum,[u"测试时间:",u"开始时间","",u"结束时间",""],testTitle)

    summaryStartRow += 1
    write(summaryStartRow, startColum, u"测试人员:",testTitle)
    write_merge(summaryStartRow, summaryStartRow, startColum+1, startColum+2,u"", testscopeStyle)

    summaryStartRow += 1
    write_merge(summaryStartRow,summaryStartRow,startColum,1,u"测试范围:",testTitle)
    write_merge(summaryStartRow+1, summaryStartRow+4, startColum+1, 10, testscope, scopecontentStyle)

    summaryStartRow += 5
    write_merge(summaryStartRow,summaryStartRow,startColum,1,u"测试结论:",testTitle)
    write(summaryStartRow,2,u"通过", testResultStyle)

    summaryStartRow += 1
    #write_merge(summaryStartRow,summaryStartRow+4,startColum,1,u"问题总结",issueSummaryStyle)
    write_merge(summaryStartRow, summaryStartRow+4, startColum+1, 10, issueSummary, scopecontentStyle)
    startRow = summaryStartRow + 6

    summaryStartRow += 5
    write_merge(summaryStartRow, summaryStartRow , startColum, 1, u"问题总结:", testTitle)
    
    summaryStartRow += 1
    write_merge(summaryStartRow, summaryStartRow , startColum + 1, 10, u"本次测试共发现问题"+str(int(bugDetail["total"]))+u"个，已修复"+str(int(bugDetail["total"]-bugDetail["open"]))+u"个，未修复"+str(int(bugDetail["open"]))+u"个.",scopecontentStyle)

    endColum = 8
    for colum in range(startColum,endColum):
        sheet.col(colum).width = 256 * 15
    for colum in range(endColum,endColum+3):
        sheet.col(colum).width = 200 * 15

    index = startRow + 2
    resolutionTitle = "Bug priority Distribution"
    if(bugDetail["Blocker"])!=0:
        if(bugDetail["Trivial"])==0:
            DrawPic.drawBugDistributionPie(["Minor","Blocker","Critical", "Major" ],[bugDetail["Minor"], bugDetail["Blocker"],bugDetail["Critical"], bugDetail["Major"]], resolutionTitle)
        else:
            DrawPic.drawBugDistributionPie(["Minor", "Blocker", "Critical", "Major","Trivial"],[bugDetail["Minor"], bugDetail["Blocker"], bugDetail["Critical"],bugDetail["Major"],bugDetail["Trivial"]], resolutionTitle)
    else:
        if(bugDetail["Trivial"])!=0:
            DrawPic.drawBugDistributionPie(["Minor","Critical", "Major" ,"Trivial"],[bugDetail["Minor"], bugDetail["Critical"], bugDetail["Major"],bugDetail["Trivial"]], resolutionTitle)
        else:
            DrawPic.drawBugDistributionPie(["Minor", "Critical", "Major"],[bugDetail["Minor"], bugDetail["Critical"], bugDetail["Major"]], resolutionTitle)
    write(index - 1, startColum, u"Bug分布图", testTitle)
    insertPic(resolutionTitle + ".bmp", index, startColum)

 # resolution
    fracs=[]
    lable=[]
    if (bugDetail["Jira"]) == "oldJira":
        lable = ["fixed", "unresolved", "wontFix"]
        list1=["cannotReproduce","duplicate","incomplete","designed"]
        for key in list1:
            if bugDetail[key]!=0:
                lable.append(key)
    else:
        list1 = ["cannotReproduce", "duplicate", "incomplete", "unresolved"]
        lable=["fixed", "wontFix", "fixInFuture"]
        for key in list1:
            if bugDetail[key]!=0:
                lable.append(key)
    resolutionTitle = "Bug resolution Distribution"
    for key in lable:
        fracs.append(bugDetail[key])
    DrawPic.drawBugDistributionPie(lable,fracs,resolutionTitle)
    insertPic(resolutionTitle + ".bmp", index, startColum+4)

    index += 21
    write(index,startColum,u"未修复的问题",testTitle)
    index += 1

    write(index, startColum, u"Bug ID", bugStyle)
    write_merge(index, index, startColum + 1, startColum + 4, u"Title", bugStyle)
    write(index, startColum+5,u"Assignee", bugStyle)
    write(index, startColum+6,u"Reporter", bugStyle)
    write(index, startColum + 7, u"Priority", bugStyle)

    bugUrl = projectUtils.BASEURL["oldJira"] + "/browse/"
    for bug in openBug:
        index += 1
        click = bugUrl + bug[0]
        write(index, startColum, xlwt.Formula('HYPERLINK("%s";"%s")' % (click,bug[0])), hyperlinkStyle)
        write_merge(index,index,startColum+1,startColum+4,bug[1],tableStyle)
        write(index,startColum + 5, bug[2], tableStyle)
        write(index, startColum + 6,bug[3], tableStyle)
        write(index, startColum+7, bug[4], tableStyle)

    index+=len(openBug)+4
    write(index, startColum, u"全部问题", testTitle)
    index+=1
    write(index, startColum, u"Bug ID", bugStyle)
    write_merge(index, index, startColum + 1, startColum + 4, u"Title", bugStyle)
    write(index, startColum+5,u"Assignee", bugStyle)
    write(index, startColum+6,u"Reporter", bugStyle)
    write(index, startColum + 7, u"Priority", bugStyle)
    write(index, startColum + 8, u"Status", bugStyle)
    write(index, startColum + 9, u"Resolution", bugStyle)
    for bug in allBug:
        index += 1
        click = bugUrl + bug[0]
        write(index, startColum, xlwt.Formula('HYPERLINK("%s";"%s")' % (click,bug[0])), hyperlinkStyle)
        write_merge(index,index,startColum+1,startColum+4,bug[1],tableStyle)
        write(index,startColum + 5, bug[2], tableStyle)
        write(index, startColum + 6,bug[3], tableStyle)
        write(index, startColum+7, bug[4], tableStyle)
        write(index, startColum + 8,bug[5], tableStyle)
        write(index, startColum+9, bug[6], tableStyle)

    index += 3
    write_merge(index,index,startColum,1,u"测试环境:",testTitle)
    write_merge(index+1, index+5, startColum+1, 10, testenv, scopecontentStyle)

    saveBook(title)




class StyleUtils:
    def __init__(self):
        self.testTitle=self.createStyle(fontHeight=230,fontName=u"宋体",setborders=False,fontColor=30,botomnAlignment=False,centerAlignment=False)
        self.reportTitleStyle = self.createStyle(fontHeight = 350,fontBold = True,fontColor=18,foreColor=47,centerAlignment=True,setborders = False)
        self.testresultStyle = self.createStyle(fontHeight=230,fontColor=17,setborders = False,setBottomLine = False)
        self.testscopeStyle = self.createStyle(fontHeight=230,fontName=u"宋体",setborders = False,setBottomLine = False,centerAlignment=True)
        self.issueSummaryStyle = self.createStyle(fontHeight=230, fontName=u"宋体", setborders=False,centerAlignment=False,botomnAlignment=False)
        self.scopecontentStyle = self.createStyle(fontHeight=220,fontName=u"宋体",setborders = False,botomnAlignment = False)
        self.cycleStyle = self.createStyle(fontHeight=200,fontBold = True,setborders = False)
        self.cycleNameStyle = self.createStyle(fontHeight=200,fontBold=True,fontColor=4,setborders=False,fontUnderline=True)
        self.dateStyle = self.createStyle(setborders=False)
        self.tableStyle = self.createStyle()
        self.passStyle = self.createStyle(foreColor=17)
        self.failStyle = self.createStyle(foreColor=10)
        self.blockStyle = self.createStyle(foreColor=52)
        self.notrunStyle = self.createStyle(foreColor=22)
        self.bugStyle = self.createStyle(foreColor=44)
        self.hyperlinkStyle = self.createStyle(fontColor=4,fontUnderline=True)

    def createStyle(self,fontHeight = 220,fontBold = False,fontName = u"Arial", fontColor = 0,fontUnderline = False, setborders = True,centerAlignment = False,alignmentWrap = True,foreColor = 1,setBottomLine = False,botomnAlignment = False,setTopLine=False):
        style = xlwt.XFStyle()
        font = xlwt.Font()
        font.height = fontHeight
        font.bold = fontBold
        font.name = fontName
        font.colour_index = fontColor
        if fontUnderline:
            font.underline = True
        style.font = font

        pattern = xlwt.Pattern()
        pattern.pattern = xlwt.Pattern.SOLID_PATTERN
        pattern.pattern_fore_colour = foreColor
        style.pattern = pattern

        if setborders:
            borders = xlwt.Borders()
            borders.left = xlwt.Borders.THIN  # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
            borders.right = xlwt.Borders.THIN
            borders.top = xlwt.Borders.THIN
            borders.bottom = xlwt.Borders.THIN
            borders.bottom_colour = 0x40
            style.borders = borders
        alignment = xlwt.Alignment()
        if centerAlignment:
            alignment.horz = xlwt.Alignment.HORZ_CENTER
        else:
            alignment.horz = xlwt.Alignment.HORZ_LEFT
        alignment.vert = xlwt.Alignment.VERT_CENTER
        if alignmentWrap:
            alignment.wrap = 1
        if botomnAlignment:
            alignment.vert = xlwt.Alignment.VERT_BOTTOM
        else:
            alignment.vert=xlwt.Alignment.VERT_TOP
        style.alignment = alignment

        if setBottomLine:
            borders = xlwt.Borders()
            borders.bottom = xlwt.Borders.THIN
            borders.bottom_colour = 0x40
            style.borders = borders

        if setTopLine:
            borders.top = xlwt.Borders.THIN
            borders.top_colour = 0x40
            style.borders = borders

        return style

    @property
    def reportTitleStyle(self):
        return self.reportTitleStyle

    @property
    def testResultStyle(self):
        return self.testresultStyle
    @property
    def testscopeStyle(self):
        return self.testscopeStyle
    @property
    def scopecontentStyle(self):
        return self.scopecontentStyle
    @property
    def cycleStyle(self):
        return self.cycleStyle
    @property
    def cycleNameStyle(self):
        return self.cycleNameStyle
    @property
    def dateStyle(self):
        return self.dateStyle
    @property
    def tableStyle(self):
        return self.tableStyle
    @property
    def passStyle(self):
        return self.passStyle
    @property
    def failStyle(self):
        return self.failStyle
    @property
    def blockStyle(self):
        return self.blockStyle
    @property
    def notrunStyle(self):
        return self.notrunStyle
    @property
    def bugStyle(self):
        return self.bugStyle
    @property
    def hyperlinkStyle(self):
        return self.hyperlinkStyle
    @property
    def issueSummaryStyle(self):
        return self.issueSummaryStyle
    @property
    def testTitle(self):
        return self.testTitle


book = initBook()
sheet = createSheet()
sheet.show_grid = False
'''
for i in range(0,100):
    styletest = xlwt.XFStyle()
    pattern = xlwt.Pattern()
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
    pattern.pattern_fore_colour = i
    styletest.pattern = pattern
    write(100,i,i,styletest)

'''
